/**
 * <p>Title: ServiceZMCM22.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 贾雪民
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-13 下午07:30:43  贾雪民  Create
 *</pre>
 */

package com.baosight.sczx.zm.cm.service;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.User;
import com.baosight.sczx.common.zm.cInterface.cm.MaterialType;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.common.zm.cInterface.cs.SystemSettingCM;
import com.baosight.sczx.zm.cm.domain.ZMCMMaterialRation;
import com.baosight.sczx.zm.cm.domain.ZMCMTeammaterialplan;
import com.baosight.sczx.zm.cm.domain.ZMCMTeammaterialplanDetail;

/**区队月需求计划管理
 * <p>Title: ServiceZMCM22.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 贾雪民
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-13 下午07:30:43  贾雪民  Create
 *</pre>
 */

public class ServiceZMCM22 extends ServiceEPBase{

	/**初始数据
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM22</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-13 下午07:30:43  贾雪民
	 * </p>
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		EiInfo outInfo = this.query(inInfo);
		outInfo.addBlock(getPlanTypeBlockBy(InitialData.getInstance().getEiBlockByNo("cmes.cm.planType")));
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.units"));
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cm.financeClassify"));
		outInfo.addBlock(User.getInstance().getConstructionStaff("cmes.cm.people", "empCode", "empName", false));
		outInfo.addBlock(MaterialType.getMateroalName());
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cm.department", "orgCode", "orgName", false));
		outInfo.set("orgCode",UserSession.getInSessionProperty("orgCode"));//当前用户所属单位ID
		outInfo.set("orgName",UserSession.getInSessionProperty("orgName"));//当前用户所属单位
		outInfo.set("userId", UserSession.getUserId());//当前登录用户ID
		if(SystemSettingCM.getInstance().getDepartmentId().equals("NULL"))
		{
			outInfo.setStatus(-1);
			outInfo.setMsg("请在系统基础设置的材料使用设置中设定计划审核单位");
			return outInfo;
		}
		outInfo.set("fdAuditDepartmentId",SystemSettingCM.getInstance().getDepartmentId());//审核单位ID
		outInfo.set("isNeedApprover", SystemSettingCM.getInstance().getIsApprover());
		return outInfo;
	}

	/**列表查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM22</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-13 下午07:30:43  贾雪民
	 * </p>
	 *
	*/	
	@Override
	public EiInfo query(EiInfo inInfo) 
	{
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		String subStr = inInfo.getString("fdParentId");
		String temp = new String(subStr);
		int tempLength = temp.length();
		String year = (subStr).substring(0,4);//树节点
		String month = (subStr).substring(4,tempLength);
		//添加查询条件
		StaticTools.setQueryParameters(inInfo, "fdYear", year, true);//年份
		StaticTools.setQueryParameters(inInfo, "fdMonth", month, true);//月份
		StaticTools.setQueryParameters(inInfo,"fdApplicationDeparment",UserSession.getInSessionProperty("orgCode").toString(),true);//当前部门
		//StaticTools.setQueryParameters(inInfo,"fdApprovePeople",UserSession.getUserId().toString(),true);//审批人
		EiInfo outInfo = super.query(inInfo, "ZMCMTeammaterialplan.query",new ZMCMTeammaterialplan());		
		return outInfo;
	}
	
	/**列表新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM14</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-13 下午07:30:43  贾雪民
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
//		校验当月计划是否已存在
		boolean validateValue = monthvalidate(inInfo, "ZMCMTeammaterialplan.monthvalidate", new HashMap<String, String>());
		
		if(validateValue) {
			return inInfo;
		}
		else {
		inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("ZMCMTeammaterialplan"));//主键
		inInfo.getBlock("result").setCell(0, "fdPlanStatus", "未上报");
		inInfo.getBlock("result").setCell(0, "fdIsgenerateStockPlan", "否");
		inInfo.getBlock("result").setCell(0, "fdEditor", UserSession.getUserId());
		EiInfo outInfo = super.insert(inInfo, "ZMCMTeammaterialplan.insert");
		return outInfo;
		}
	}

	/**修改
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM22</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-13 下午07:30:43  贾雪民
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdKeyId", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
		boolean validateValue = monthvalidate(inInfo, "ZMCMTeammaterialplan.monthvalidate", params);
		if(validateValue) {
			return inInfo;
		}
		EiInfo outInfo = super.update(inInfo, "ZMCMTeammaterialplan.update");
		return outInfo;
	}	

	/**修改计划状态
	 * <p>Function: update_fdPlanStatus</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM22</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-13 下午06:30:43  贾雪民
	 * </p>
	 *
	 */
	public EiInfo update_fdPlanStatus(EiInfo inInfo) {
		String fdPlanId = inInfo.addBlock("result").getCellStr(0, "fdPlanId");
		inInfo.getBlock("result").setCell(0, "fdPlanId",fdPlanId);
		EiInfo outInfo = super.update(inInfo, "ZMCMTeammaterialplan.update_fdPlanStatus");
		return outInfo;
	}	
	
	/**删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM22</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-13 下午07:30:43  贾雪民
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		for(int i=0; i<inInfo.getBlock("result").getRowCount();i++)
		{
			String fdKeyId=inInfo.getBlock("result").getCellStr(i, "fdKeyId");
			Map map=new HashMap();
			map.put("fdPlanId", fdKeyId);
			List list=dao.query("ZMCMTeammaterialplanDetail.query", map);
			if(list.size()>0)
			{
				inInfo.setStatus(-1);
				inInfo.setMsg("明细表有记录，不可删除主表记录！");
				return inInfo;
			}
		}
		EiInfo _temp =super.delete(inInfo,"ZMCMTeammaterialplan.delete","result");
		EiInfo outInfo = this.query(inInfo);
		outInfo.setMsg(_temp.getMsg());
		outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 上报
	 * <p>Function: report</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-13 下午07:30:43  贾雪民
	 *    2012-2-22 wangkun 增加上报时根据费用额度进行限制功能
	 * </p>
	 *
	 */
	public EiInfo report(EiInfo inInfo) 
	{
		String fdApplicationDeparment = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdApplicationDeparment");
		String fdKeyId = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdKeyId");
		String fdYear = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdYear");
		String fdMonth = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdMonth");
		Double fdPlanSum = new Double(0);
		Map inMap = new HashMap();
		inMap.put("fdKeyId", fdKeyId);
		inMap.put("fdFinanceClassify", "材料费用");
		List currentplanList = dao.query("ZMCMTeammaterialplanDetail.queryPlanSum", inMap);
		fdPlanSum = Double.parseDouble(((Map)currentplanList.get(0)).get("fdPlanSum").toString());
		
		inMap = new HashMap();
		inMap.put("fdDepartmentId", fdApplicationDeparment);
		inMap.put("fdYear", fdYear);
		inMap.put("fdMonth", fdMonth);
		List _list = dao.query("ZMCMMaterialRation.query", inMap);
		if(_list.size() <= 0)
		{
			inInfo.setMsg("本单位没有设置费用额度，不能上报需求计划，请联系费用额度管理部门！");
			inInfo.setStatus(-1);
			return inInfo;
		}
		else
		{
			ZMCMMaterialRation ration = (ZMCMMaterialRation)_list.get(0);
			
			inMap.put("fdApplicationDeparment", fdApplicationDeparment);
			inMap.put("fdFinanceClassify", "材料费用");
			inMap.put("isSubmit", "yes");
			List plansumList = dao.query("ZMCMTeammaterialplanDetail.queryPlanSum",inMap);
			Double allPlanSum = Double.parseDouble(((HashMap)plansumList.get(0)).get("fdPlanSum").toString());
			
			if((ration.getFdMonthRation() - (allPlanSum + fdPlanSum)) < 0)
			{
				Double value = (allPlanSum + fdPlanSum) - ration.getFdMonthRation();
				inInfo.setMsg("上报计划中的资金分类为“材料费用”的物资总价超出本月剩余的费用额度：" + new DecimalFormat("0.00000").format(value) + 
									"元，不能上报该计划，请联系费用额度管理部门调整额度！");
				inInfo.setStatus(-1);
				return inInfo;
			}
			else
			{
				EiInfo _temp = super.update(inInfo, "ZMCMTeammaterialplan.report");
				EiInfo  outInfo = this.query(inInfo);
			    outInfo.setMsg(_temp.getMsg());
			    outInfo.setDetailMsg(_temp.getDetailMsg());
			    return outInfo;
			}
		}
	}	

	/**
	 * 撤回
	 * <p>Function: qxreturn</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-13 下午07:30:43  贾雪民
	 * </p>
	 *
	 */
	public EiInfo qxreturn(EiInfo inInfo) {
		  EiInfo _temp = super.update(inInfo, "ZMCMTeammaterialplan.qxreturn");
		  EiInfo  outInfo = this.query(inInfo);
	      outInfo.setMsg(_temp.getMsg());
	      outInfo.setDetailMsg(_temp.getDetailMsg());
	      return outInfo;
	}	

	/**
	 * 审批
	 * <p>Function: approve</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-13 下午07:30:43  贾雪民
	 * </p>
	 *
	 */
	public EiInfo approve(EiInfo inInfo) {
		  EiInfo _temp = super.update(inInfo, "ZMCMTeammaterialplan.approve");
		  EiInfo  outInfo = this.query(inInfo);
	      outInfo.setMsg(_temp.getMsg());
	      outInfo.setDetailMsg(_temp.getDetailMsg());
	      return outInfo;
	}
	
	/**
	 * 审核
	 * <p>Function: audit</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-13 下午07:30:43  贾雪民
	 * </p>
	 *
	 */
	public EiInfo audit(EiInfo inInfo) {
		  EiInfo _temp = super.update(inInfo, "ZMCMTeammaterialplan.audit");
		  EiInfo  outInfo = this.query(inInfo);
	      outInfo.setMsg(_temp.getMsg());
	      outInfo.setDetailMsg(_temp.getDetailMsg());
	      return outInfo;
	}

	/**
	 *
	 * 校验重复
	 * <p>Function: monthvalidate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-24 下午01:30:43  贾雪民
	 * </p>
	 *
	 */	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private boolean monthvalidate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		String plantype = inInfo.getBlock("result").getCellStr(0, "fdPlanType"); //当前计划类别
		HashMap inMap = new HashMap();		
		inMap.put("fdUserTypeName", inInfo.getBlock("result").getCellStr(0, "fdPlanType"));
		List _list = dao.query("ZMCMPlanTypeRelation.validate", inMap);
		if(_list.size() > 0)
		{
			plantype = ((Map)_list.get(0)).get("fdDefaultTypeName").toString();
		}
		
		//添加查询条件
		params.put("fdYear", inInfo.getBlock("result").getCellStr(0, "fdYear"));//当前年份
		params.put("fdMonth", inInfo.getBlock("result").getCellStr(0, "fdMonth"));//当前月份
		params.put("fdPlanType", inInfo.getBlock("result").getCellStr(0, "fdPlanType"));//计划类型
		params.put("fdApplicationDeparment",UserSession.getInSessionProperty("orgCode").toString() );//当前部门
		
		long ydcs=SystemSettingCM.getInstance().getMonthPlan();//月度计划份数
	    long zxcs=SystemSettingCM.getInstance().getMiddlePlan();//中旬计划份数
	    long jycs=SystemSettingCM.getInstance().getUrgentPlan();//急用计划份数
	    
	    long ydBeginDate= Integer.parseInt(SystemSettingCM.getInstance().getPlanBeginDate());//月度计划开始日
	    long ydEndDate=SystemSettingCM.getInstance().getMonthPlanDate();//月度计划截止日期
	    long zxEndDate=SystemSettingCM.getInstance().getMiddlePlanDate();//中旬计划截止日
	    long zxBeginDate=SystemSettingCM.getInstance().getUrgentPlanDate();//中旬计划开始日
		List result = dao.query(sqlName, params);
		String msg="";
		boolean status=false; 
		int cs=result.size(); //取得当前计划类型已经存在的记录数
		System.out.println("cs:"+cs);
		long date = Integer.parseInt(inInfo.getBlock("result").getCellStr(0, "fdApplicationDate").substring(8, 10));
		if (plantype.equals("月度计划"))
		{
		    if (cs>=ydcs) 
		    {
		        msg="月度计划已经达到系统设置中允许上报的份数，不可再次新增!";
		        status=true;
				inInfo.setStatus(-1);
		    }
		    if(ydBeginDate !=0 && ydEndDate !=0)
		    {
		    	if(date < ydBeginDate ||date > ydEndDate)
		    	{
		    		msg="月度计划申请日期不在系统设置中设定的日期区间，不可新增!";
		            status=true;
					inInfo.setStatus(-1);
		    	}
		    }else
		    {
		    	if(ydBeginDate == 0)
		    	{
		    		if(date > ydEndDate)
		    		{
		    			msg="月度计划申请日期已经超过系统设置的月度计划截止日，不可新增!";
		                status=true;
		    			inInfo.setStatus(-1);
		    		}
		    	}else
		    	{
		    		if(date < ydBeginDate)
		    		{
		    			msg="月度计划申请日期在系统设置的月度计划开始日之前，不可新增!";
		                status=true;
		    			inInfo.setStatus(-1);
		    		}
		    	}
		    }
		}
		else if (plantype.equals("中旬计划"))
		{
		    if (cs>=zxcs)
		    {
		    	msg="中旬计划已经达到系统设置中允许上报的份数，不可再次新增!";
		    	status=true;
				inInfo.setStatus(-1);
		    }
		    
		    if(zxEndDate !=0 && zxBeginDate !=0)
		    {
		    	if(date < zxBeginDate ||date > zxEndDate)
		    	{
		    		msg="中旬计划申请日期不在系统设置中设定的日期区间，不可新增!";
		            status=true;
					inInfo.setStatus(-1);
		    	}
		    }
		    else
		    {
		    	if(zxBeginDate == 0)
		    	{
		    		if(date > zxEndDate)
		    		{
		    			msg="中旬计划申请日期已经超过系统设置的中旬计划截止日，不可新增!";
		                status=true;
		    			inInfo.setStatus(-1);
		    		}
		    	}else
		    	{
		    		if(date < zxBeginDate)
		    		{
		    			msg="中旬计划申请日期在系统设置的中旬计划开始日之前，不可新增!";
		                status=true;
		    			inInfo.setStatus(-1);
		    		}
		    	}
		    }
		}
		else if(plantype.equals("急用计划"))
		{
		    if (cs>=jycs)
		    {
		    	msg="急用计划已经达到系统设置中允许上报的份数，不可再次新增!";
		    	status=true;
				inInfo.setStatus(-1);
		    }
		}
		else
		{
			status=false;
		}
		
		inInfo.setMsg(msg);
		return status;
	}	
	
	/**
	 *
	 * 根据系统设置的计划类型份数来判断计划类型是否可用
	 * 默认的计划类型有月度计划、中旬计划、急用计划
	 * <p>Function: monthvalidate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-9-15 下午01:30:43  高强
	 * </p>
	 *
	 */	
	public EiBlock getPlanTypeBlockBy(EiBlock block){
		long ydcs=SystemSettingCM.getInstance().getMonthPlan();//月度计划份数
	    long zxcs=SystemSettingCM.getInstance().getMiddlePlan();//中旬计划份数
	    long jycs=SystemSettingCM.getInstance().getUrgentPlan();//急用计划份数
		for(int i=0; i<block.getRowCount(); i++){
			if(block.getRow(i).get("display").equals("月度计划"))
			{
				if(ydcs == 0)
				{
					block.getRows().remove(i);
				}
			}else if(block.getRow(i).get("display").equals("中旬计划"))
			{
				if(zxcs == 0)
				{
					block.getRows().remove(i);
				}
			}else if(block.getRow(i).get("display").equals("急用计划"))
			{
				if(jycs == 0)
				{
					block.getRows().remove(i);
				}
			}
		}
		return block;
	}
	
	/**
	 * 
	 * <p>Function: queryAfterUpdate</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-2上午03:21:07 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public EiInfo queryAfterUpdate(EiInfo inInfo) {
		Map inMap = new HashMap();
		inMap.put("fdKeyId", inInfo.get("fdKeyId"));
		List _list = dao.query("ZMCMTeammaterialplan.queryPlanSum", inMap);
		
		EiInfo outInfo = new EiInfo();
		if(_list.size() > 0)
		{
			Map plan = (Map)_list.get(0);
			outInfo.set("fdPlanSum", plan.get("fdPlanSum"));
			outInfo.set("fdMaterialSum", plan.get("fdMaterialSum"));
		}
		
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: copyLastMonthPlan</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-2上午03:21:24 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public EiInfo copyLastMonthPlan(EiInfo inInfo)
	{
		boolean validateValue = validateCopyPlan(inInfo);
		
		if(validateValue) 
		{
			return inInfo;
		}
		
		EiInfo planInfo = new EiInfo();
		planInfo.addBlock(EiConstant.resultBlock);
		
		EiInfo planDetailInfo = new EiInfo();
		planDetailInfo.addBlock(EiConstant.resultBlock);
		
		EiBlock block = inInfo.getBlock(EiConstant.resultBlock);
		for(int i=0; i<block.getRowCount(); i++)
		{
			Map row = block.getRow(i);
			String lastKeyId = (String)row.get("fdKeyId");
			String lastYear = (String)row.get("fdYear");
			String lastMonth = (String)row.get("fdMonth");
			String newKeyId = TablePk.getInstance().getNextValue("T_CM_TEAMMATERIALPLAN");
			String newYear = StaticTools.getYear();
			String newMonth = Integer.parseInt(StaticTools.getMonth())+"";
			
			if(Integer.parseInt(lastMonth) == 12)
			{
				newYear = (Integer.parseInt(lastYear) + 1) + "";
				newMonth = "1";
			}
			else
			{
				newYear = lastYear;
				newMonth = (Integer.parseInt(lastMonth) + 1) + "";
			}
			row.put("fdKeyId", newKeyId);
			row.put("fdPlanId", newKeyId);
			row.put("fdYear", newYear);
			row.put("fdMonth", newMonth);
			row.put("fdApplicationDate", StaticTools.getDate());
			row.put("fdPlanStatus", "未上报");
			planInfo.addRow(EiConstant.resultBlock, row);
			
			Map inMap = new HashMap();
			inMap.put("fdPlanId", lastKeyId);
			List<ZMCMTeammaterialplanDetail> _list = dao.query("ZMCMTeammaterialplanDetail.query", inMap);
			
			for(int j=0; j<_list.size(); j++)
			{
				ZMCMTeammaterialplanDetail detail = _list.get(j);
				
				Map detail_row = new HashMap();
				detail_row.put("fdKeyId", TablePk.getInstance().getNextValue("T_CM_TEAMMATERIALPLAN_DETAIL"));
				detail_row.put("fdPlanId", newKeyId);
				detail_row.put("fdMaterialId", detail.getFdMaterialId());
				detail_row.put("fdMaterialCode", detail.getFdMaterialCode());
				detail_row.put("fdMaterialInnerCode", detail.getFdMaterialInnerCode());
				detail_row.put("fdMaterialName", detail.getFdMaterialName());
				detail_row.put("fdTypeCode", detail.getFdTypeCode());
				detail_row.put("fdMaterialType", detail.getFdMaterialType());
				detail_row.put("fdFinanceClassify", detail.getFdFinanceClassify());
				detail_row.put("fdQuality", detail.getFdQuality());
				detail_row.put("fdSpecModel", detail.getFdSpecModel());
				detail_row.put("fdNumber", detail.getFdNumber());
				detail_row.put("fdMetric", detail.getFdMetric());
				detail_row.put("fdApplicationNum", detail.getFdApplicationNum());
				detail_row.put("fdAuditNum", detail.getFdApplicationNum());
				detail_row.put("fdPurchaseNum", detail.getFdApplicationNum());
				detail_row.put("fdSum", detail.getFdSum());
				detail_row.put("fdParameter", detail.getFdParameter());
				detail_row.put("fdProjectname", detail.getFdProjectname());
				detail_row.put("fdProjectId", detail.getFdProjectId());
				detail_row.put("fdRequireDate", detail.getFdRequireDate());
				detail_row.put("fdPrice", detail.getFdPrice());
				detail_row.put("fdSerialNum", detail.getFdSerialNum());

				planDetailInfo.addRow(EiConstant.resultBlock, detail_row);
			}
		}
		EiInfo outInfo = super.insert(planInfo, "ZMCMTeammaterialplan.insert");
		if(outInfo.getStatus() != 1)
		{
			return outInfo;
		}
		
		outInfo = super.insert(planDetailInfo, "ZMCMTeammaterialplanDetail.insert");
		if(outInfo.getStatus() != 1)
		{
			return outInfo;
		}
		
		outInfo = super.update(planInfo, "ZMCMTeammaterialplan.updatezhubiao");
		if(outInfo.getStatus() != 1)
		{
			return outInfo;
		}
		
		return outInfo;
	}
	
	@SuppressWarnings("rawtypes")
	private boolean validateCopyPlan(EiInfo inInfo)
	{
		String msg = "";
		boolean status = false;
		boolean month_status = false;
		boolean middle_status = false;
		boolean hurry_status = false;
		long month_num = 0;//用户月度计划份数
		long middle_num = 0;//用户中旬计划份数
		long hurry_num = 0;//用户急用计划份数
		long sys_month_num = SystemSettingCM.getInstance().getMonthPlan();//系统设置月度计划份数
		long sys_middle_num = SystemSettingCM.getInstance().getMiddlePlan();//系统设置中旬计划份数
		long sys_hurry_num = SystemSettingCM.getInstance().getUrgentPlan();//系统设置急用计划份数
		
		EiBlock block = inInfo.getBlock(EiConstant.resultBlock);
		for(int i=0; i<block.getRowCount(); i++)
		{
			Map row = block.getRow(i);
			String fdYear = (String)row.get("fdYear");
			String fdMonth = (String)row.get("fdMonth");
			String plantype = (String)row.get("fdPlanType");
			
			String newYear = StaticTools.getYear();
			String newMonth = Integer.parseInt(StaticTools.getMonth())+"";
			
			if(Integer.parseInt(fdMonth) == 12)
			{
				newYear = (Integer.parseInt(fdYear) + 1) + "";
				newMonth = "1";
			}
			else
			{
				newYear = fdYear;
				newMonth = (Integer.parseInt(fdMonth) + 1) + "";
			}

			HashMap<String, String> inMap = new HashMap<String, String>();		
			inMap.put("fdUserTypeName", plantype);
			List _list = dao.query("ZMCMPlanTypeRelation.validate", inMap);
			if(_list.size() > 0)
			{
				plantype = ((Map)_list.get(0)).get("fdDefaultTypeName").toString();
			}
			else
			{
				plantype = "";
			}
			
			//添加查询条件
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdYear", newYear);//当前年份
			params.put("fdMonth", newMonth);//当前月份
			params.put("fdPlanType", (String)row.get("fdPlanType"));//计划类型
			params.put("fdApplicationDeparment", UserSession.getInSessionProperty("orgCode").toString());//当前部门
			
			List result = dao.query("ZMCMTeammaterialplan.monthvalidate", params);

			if (plantype.equals("月度计划"))
			{
				if(result.size() == 0)
				{
					month_num += 1;
					month_status = true;
				}
				else
				{
					if(!month_status)
					{
						month_num += result.size()+1;
						month_status = true;
					}
					else
					{
						month_num += result.size(); //取得当前计划类型已经存在的记录数
					}
				}
			}
			else if (plantype.equals("中旬计划"))
			{
				if(result.size() == 0)
				{
					middle_num += 1;
					middle_status = true;
				}
				else
				{
					if(!middle_status)
					{
						middle_num += result.size()+1;
						middle_status = true;
					}
					else
					{
						middle_num += result.size(); //取得当前计划类型已经存在的记录数
					}
				}
			}
			else if(plantype.equals("急用计划"))
			{
				if(result.size() == 0)
				{
					hurry_num += 1;
					hurry_status = true;
				}
				else
				{
					if(!hurry_status)
					{
						hurry_num += result.size()+1;
						hurry_status = true;
					}
					else
					{
						hurry_num += result.size(); //取得当前计划类型已经存在的记录数
					}
				}
			}
		}
		
	    if (month_num > sys_month_num) 
	    {
	        msg = "月度计划已经达到系统设置中允许上报的份数，不可复制，请重新选择!";
	        status = true;
			inInfo.setStatus(-1);
	    }
	    if (middle_num > sys_middle_num)
	    {
	    	msg = "中旬计划已经达到系统设置中允许上报的份数，不可复制，请重新选择!";
	    	status = true;
			inInfo.setStatus(-1);
	    }
	    if (hurry_num > sys_hurry_num)
	    {
	    	msg = "急用计划已经达到系统设置中允许上报的份数，不可复制，请重新选择!";
	    	status = true;
			inInfo.setStatus(-1);
	    }
		
		inInfo.setMsg(msg);
		return status;
	}
}
